<template>
  <div class="default-main ba-table-box">
    <TableHeader
      :buttons="['refresh', 'add', 'remove', 'comSearch', 'columnDisplay']"
      :quick-search-placeholder="placeholder"
      @action="baTable.onTableHeaderAction"
    />
    <Table ref="tableRef" @action="baTable.onTableAction" />
    <!-- 表单 -->
    <PopupForm />
  </div>
</template>
<script lang="ts" setup>
import { ref, provide, onMounted } from "vue";
import baTableClass from "@/utils/baTable";
import { baTableApi, operatorShowOff } from "@/utils/common"; // 导入表格api方法生成器
import { Dictionary } from "@/api/backend/index";
import Table from "@/components/table/index.vue";
import TableHeader from "@/components/table/header/index.vue";
import PopupForm from "../popupForm.vue";
import useCurrentInstance from "@/utils/useCurrentInstance";
import type { TableRow, TableColumn } from "@/utils/baTable"; //表格列行类型
import { useDictionary } from "@/store/backend/tool/dictionary";
import { useRoute } from "vue-router";
import { useNavTabs } from "@/store/navTabs";

const placeholder = ref<string>("模糊搜索");
const tableRef = ref();
const dictionary = useDictionary();
const route = useRoute();
const navTabs = useNavTabs();

//字段显隐记忆功能
const filedShowOff = (filter: any) => {
  if ((navTabs as any).columnDisplayStoreMap) {
    return (navTabs as any).columnDisplayStoreMap.get(
      route.path + "/" + filter
    );
  }
  return true;
};
// 每页个数记忆功能
const pageSizeKeep = () => {
  if ((navTabs as any).pageSizeMap) {
    return (navTabs as any).pageSizeMap.get(route.path + Dictionary)
      ? (navTabs as any).pageSizeMap.get(route.path + Dictionary)
      : 10;
  }
  return 10;
};

const baTable = new baTableClass(
  new baTableApi(Dictionary), //this.api = api部门管理
  {
    pk: "dictId",
    column: [
      { type: "selection", align: "center", operator: false },
      {
        show: filedShowOff("dictId"),
        label: "字典主键",
        prop: "dictId",
        align: "left",
        width: "90",
        operator: false,
      },
      {
        show: filedShowOff("dictName"),
        label: "字典名称",
        prop: "dictName",
        align: "left",
      },
      {
        show: filedShowOff("dictType"),
        label: "字典类型",
        prop: "dictType",
        align: "left",
        operator: false,
      },
      {
        show: filedShowOff("status"),
        label: "状态",
        prop: "status",
        align: "center",
        width: "80",
        render: "tag",
        custom: { 0: "success", 1: "danger" },
        replaceValue: {
          0: "正常",
          1: "停用",
        },
      },
      {
        show: operatorShowOff([route.path + "/edit", route.path + "/remove"]),
        label: "操作",
        align: "center",
        width: "130",
        operator: false,
        render: "buttons",
        fixed: "right",
        buttons: [
          {
            render: "tipButton",
            name: "editSet",
            title: "编辑",
            text: "",
            type: "primary",
            icon: "iconfont icon-pencil",
            class: "table-row-edit",
            disabledTip: false,
            // 按钮是否显示，请返回布尔值
            display: (row: TableRow, field: TableColumn) => {
              return true;
            },
            // 按钮是否禁用，请返回布尔值
            disabled: (row: TableRow, field: TableColumn) => {
              return false;
            },
            attr: {},
          },
          {
            render: "tipButton",
            name: "info",
            title: "详情",
            text: "",
            type: "primary",
            icon: "el-icon-ZoomIn",
            class: "table-row-edit",
            disabledTip: false,
            // 按钮是否显示，请返回布尔值
            display: (row: TableRow, field: TableColumn) => {
              return true;
            },
            // 按钮是否禁用，请返回布尔值
            disabled: (row: TableRow, field: TableColumn) => {
              return false;
            },
            attr: {},
          },
          {
            render: "confirmButton",
            name: "removeSet",
            title: "删除",
            text: "",
            type: "danger",
            icon: "el-icon-DeleteFilled",
            class: "table-row-delete",
            popconfirm: {
              confirmButtonText: "删除",
              cancelButtonText: "取消",
              confirmButtonType: "danger",
              title: "确定删除所选项吗",
            },
            disabledTip: false,
            // 按钮是否显示，请返回布尔值
            display: (row: TableRow, field: TableColumn) => {
              return true;
            },
            // 按钮是否禁用，请返回布尔值
            disabled: (row: TableRow, field: TableColumn) => {
              return false;
            },
            attr: {},
          },
        ],
      },
    ],
    filter: {
      // pageSize: pageSizeKeep(),
    },
    extend: {
      // maxHeight: 400,
    },
  }
);
const emit = defineEmits(["on-showDictList"]);
const getDictData = (name: string, row: any) => {
  emit("on-showDictList", row);
  dictionary.$state.dictType = row.dictType;
  baTable.requestEdit(row.dictId); //获取相应id的字典数据
  baTable.form.operate = "edit"; //打开字典数据表单
};
const removeData = (row: any) => {
  dictionary.deleteDict(row).then((res) => {
    baTable.getList("");
  });
};

//打开字典项页
const infoButtonClick = (name: string, row: any) => {
  emit("on-showDictList", row);
  dictionary.$state.dictType = row.dictType;
};

onMounted(() => {
  //全局事件总线
  const { proxy } = useCurrentInstance();
  //将table的ref给到table类
  baTable.table.ref = tableRef.value;
  baTable.mount();
  baTable.getList(Dictionary);
  /**
   * 表格内的按钮响应
   * @param name 按钮name
   * @param row 被操作行数据
   */
  proxy.eventBus.on(
    "onTableButtonClick",
    (data: { name: string; row: TableRow }) => {
      if (data.name === "editSet") {
        //除了获取数据外 打开下级字典项数据
        getDictData(data.name, data.row);
      } else if (data.name === "removeSet") {
        removeData(data.row);
      } else if (data.name == "info") {
        //携带主键字段pk
        infoButtonClick(
          (data.row as TableRow)[baTable.table.pk!],
          data.row as any
        );
      }
    }
  );
});
// 实例化表格后，将 baTable 的实例提供给上下文
provide("baTable", baTable);
</script>
<style lang="scss" scoped></style>
